﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using HouseAgencySite.Models;
using HouseAgencySite.Utils;
using Webdiyer.WebControls.Mvc;

namespace HouseAgencySite.Areas.Admin.Controllers
{
    public class AreasController : BaseController
    {
        private AgencyDBContext db = new AgencyDBContext();

        //
        // GET: /Admin/Areas/      
        [CheckLogin()]
        public ActionResult Index(int pageindex = 1)
        {
            List<HouseAgencySite.Models.Areas> list = db.Areas.Where(o => o.IsDelete == false).OrderByDescending(o => o.CreateTime).ToList();
            IPagedList pageList = list.ToPagedList(pageindex, 10);
            return View(pageList);
        }

        //
        // GET: /Admin/Areas/Details/5
        [CheckLogin()]
        public ActionResult Details(string id = null)
        {
            HouseAgencySite.Models.Areas areas = db.Areas.Find(id);
            if (areas == null)
            {
                return HttpNotFound();
            }
            return View(areas);
        }

        //
        // GET: /Admin/Areas/Create
        [CheckLogin()]
        public ActionResult Create()
        {
            ViewBag.Guid = new SelectList(db.HouseInfos, "Guid", "Title");
            return View();
        }

        //
        // POST: /Admin/Areas/Create

        [HttpPost]
        [CheckLogin()]
        public ActionResult Create(HouseAgencySite.Models.Areas areas)
        {
            if (ModelState.IsValid)
            {
                areas.Guid = Guid.NewGuid().ToString();
                areas.Create(areas, CurrentUser);
                db.Areas.Add(areas);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Guid = new SelectList(db.HouseInfos, "Guid", "Title", areas.Guid);
            return View(areas);
        }
        [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]   //清除缓存
        public JsonResult CheckArea(string fullPath,string guid)
        {
            if (!string.IsNullOrEmpty(fullPath) && fullPath != "//")
            {
                if (string.IsNullOrEmpty(guid) || guid == "undefined")
                {
                    if (db.Areas.ToList().Any(o => o.FullPath == fullPath && o.IsDelete == false)) return Json(false, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    if (db.Areas.ToList().Any(o => o.FullPath == fullPath && o.Guid != guid && o.IsDelete == false)) return Json(false, JsonRequestBehavior.AllowGet);
                }
            }           
            return Json(true,JsonRequestBehavior.AllowGet);;
        }

        //
        // GET: /Admin/Areas/Edit/5
        [CheckLogin()]
        public ActionResult Edit(string id = null)
        {
            HouseAgencySite.Models.Areas areas = db.Areas.Find(id);
            if (areas == null)
            {
                return HttpNotFound();
            }
            ViewBag.Guid = new SelectList(db.HouseInfos, "Guid", "Title", areas.Guid);
            return View(areas);
        }

        //
        // POST: /Admin/Areas/Edit/5
        [CheckLogin()]
        [HttpPost]
        public ActionResult Edit(HouseAgencySite.Models.Areas areas)
        {
            if (ModelState.IsValid)
            {

                db.Entry(areas).State = EntityState.Modified;
                areas.Edit(db, areas, CurrentUser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Guid = new SelectList(db.HouseInfos, "Guid", "Title", areas.Guid);
            return View(areas);
        }

        //
        // GET: /Admin/Areas/Delete/5
        [CheckLogin()]
        public ActionResult Delete(string id = null)
        {
            HouseAgencySite.Models.Areas areas = db.Areas.Find(id);
            if (areas == null)
            {
                return HttpNotFound();
            }
            return View(areas);
        }

        //
        // POST: /Admin/Areas/Delete/5
        [CheckLogin()]
        [HttpPost, ActionName("Delete")]
        public int DeleteConfirmed(string id)
        {
            HouseAgencySite.Models.Areas areas = db.Areas.Find(id);
            db.Entry(areas).State = EntityState.Modified;
            areas.Delete(db, areas, CurrentUser);
           return db.SaveChanges();
            //return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        /// <summary>
        /// 获取地区名称
        /// </summary>
        /// <param name="areaName"></param>
        /// <param name="selectedItem"></param>
        /// <returns></returns>  
        [CheckLogin()]
        public JsonResult GetAreaNames(int areaType, string areaName, string selectedItem)
        {
            return Json(ParamHelper.Areas(areaType, areaName, selectedItem), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult Search(AreaSearchFilter filter) {
            var list = db.Areas.Where(o=>o.IsDelete==false) as IQueryable<HouseAgencySite.Models.Areas>;
            if (!string.IsNullOrEmpty(filter.City)) {
                list = list.Where(o=>o.FullPath.Contains(filter.City));
            }
            if (!string.IsNullOrEmpty(filter.Area)) {
                list = list.Where(o=>o.FullPath.Contains(filter.City+"/"+filter.Area));
            }
            if (!string.IsNullOrEmpty(filter.AddressName))
            {
                list = list.Where(o => o.FullPath.Contains(filter.City + "/" + filter.Area + "/" + filter.AddressName));
            }
            list = list.OrderByDescending(o => o.CreateTime);
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            return View("Index", list.ToList().ToPagedList(pageIndex, 10));
        }
    }
}